package com.itjin.resume_back.controller;

import com.itjin.resume_back.common.Result;
import com.itjin.resume_back.dto.LoginResponse;
import com.itjin.resume_back.entity.User;
import com.itjin.resume_back.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 * 提供用户相关的接口，包括注册、登录、修改密码等功能
 */
@Tag(name = "用户管理", description = "用户相关的接口，包括注册、登录、修改密码等功能")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

  private final UserService userService;

  /**
   * 用户注册
   * 
   * @param user 用户信息
   * @return 注册结果
   */
  @Operation(summary = "用户注册", description = "新用户注册接口")
  @PostMapping("/register")
  public Result<String> register(@RequestBody @Valid User user) {
    try {
      userService.register(user);
      return Result.success("注册成功");
    } catch (Exception e) {
      return Result.error(e.getMessage());
    }
  }

  /**
   * 用户登录
   * 
   * @param username 用户名
   * @param password 密码
   * @return 登录结果、token 和 用户信息
   */
  @Operation(summary = "用户登录", description = "用户登录接口，返回token和用户信息")
  @PostMapping("/login")
  public Result<Map<String, Object>> login(
      @Parameter(description = "用户名", required = true) @RequestParam String username,
      @Parameter(description = "密码", required = true) @RequestParam String password) {
    try {
      LoginResponse loginResponse = userService.login(username, password);
      Map<String, Object> map = new HashMap<>();
      map.put("token", loginResponse.getToken());
      map.put("user", loginResponse.getUser());
      return Result.success("登录成功", map);
    } catch (Exception e) {
      return Result.error(e.getMessage());
    }
  }

  /**
   * 修改密码
   * 
   * @param userId      用户ID
   * @param oldPassword 旧密码
   * @param newPassword 新密码
   * @return 修改结果
   */
  @Operation(summary = "修改密码", description = "用户修改密码接口")
  @PutMapping("/password")
  public Result<String> updatePassword(
      @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
      @Parameter(description = "旧密码", required = true) @RequestParam String oldPassword,
      @Parameter(description = "新密码", required = true) @RequestParam String newPassword) {
    try {
      userService.updatePassword(userId, oldPassword, newPassword);
      return Result.success("密码修改成功");
    } catch (Exception e) {
      return Result.error(e.getMessage());
    }
  }

  /**
   * 获取用户信息
   * 
   * @param userId 用户ID
   * @return 用户信息
   */
  @Operation(summary = "获取用户信息", description = "获取指定用户的基本信息")
  @GetMapping("/info")
  public Result<User> getUserInfo(
      @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
    try {
      User user = userService.getById(userId);
      if (user != null) {
        // 安全考虑，清除密码
        user.setPassword(null);
        return Result.success(user);
      } else {
        return Result.error("用户不存在");
      }
    } catch (Exception e) {
      return Result.error(e.getMessage());
    }
  }
}